<?
header("Content-Type: text/html; charset=ISO-8859-1");
include '../include/vars.php';
include '../include/functions/functions.php';

db_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_db);

$category = mysql_real_escape_string($_GET['category']);
$sub = mysql_real_escape_string($_GET['sub']);

$offset = mysql_real_escape_string($_GET['offset']) +1;
$type = $_GET['type'];

switch ($type) {

	case "news":
		if ($category) {
			$low = $category."00";
			$high = ($category+1)."00";
			if ($sub) $getcatnews = mysql_query("SELECT `news_id` FROM `news_groups` WHERE `group_id` = '$category"."$sub'");
			else $getcatnews = mysql_query("SELECT `news_id` FROM `news_groups` WHERE `group_id` BETWEEN $low AND $high");
			while ($gcn = mysql_fetch_assoc($getcatnews)) {
				$buffer[] = $gcn['news_id'];
			}
			if ($buffer) {
				$categories = implode(",",$buffer);
				$buffer=null;
				$catsql = "`id` IN($categories) AND";
			}
			else die("Inga nyheter hittades"); //inga nyheter hittades
		}
		$get=mysql_query("SELECT SQL_CALC_FOUND_ROWS `id`, `title`, `text`, `text2`, `mainlink`, `mainlink_activated`, `date_publish`, `date_publish2`, `date_publish3`, `date_publish4`, `user_id`, `text_source`, `img_source`, `img_file`, `img_description` FROM `news` WHERE $catsql `approved`='1' AND `deleted`='0' AND NOW() BETWEEN `date_publish`	AND `date_end` ORDER BY `date_publish` DESC, `id` DESC") or die(mysql_error());
		while ($gn = mysql_fetch_assoc($get)) {
			while ($gn = mysql_fetch_assoc($get)) {
					if (!$latest) $latest = $gn;
					$date1 = $gn['date_publish'];
					$date2 = $gn['date_publish2'];
					$date3 = $gn['date_publish3'];
					$date4 = $gn['date_publish4'];
					if ($date1 > "0000-00-00 00:00:00") $news[$date1][]=$gn;
					if ($date2 > "0000-00-00 00:00:00") $news[$date2][]=$gn;
					if ($date3 > "0000-00-00 00:00:00") $news[$date3][]=$gn;
					if ($date4 > "0000-00-00 00:00:00") $news[$date4][]=$gn;

				}
				$i=0;
				krsort($news);
				foreach ($news as $date=>$n) {
					if ($date >= date("Y-m-d H:i:s")) continue;
					foreach ($n as $gn) {
						if (++$i > ($offset * 60)) { $limitreached=true; break; }
						$text = strip_tags($gn['text'])." ".strip_tags($gn['text2']);
						$title = substr($gn['title'],0,40);
						$lentitle = strlen(strip_tags($title));

						if (strlen($text) + $lentitle > 70) $text = substr($text,0,(70 - $lentitle));

						echo "<div style=\"padding:4px\"><small>".substr($gn['date_publish'],0,10)."</small><br/><a href=\"?page=news";
						if ($category) echo "&cat=$category";
						if ($_GET['sub']) echo "&sub=$sub";
						echo "&id=$gn[id]\"><b>".strip_tags($title)."</b>. $text &gt;&gt;</a></div>";
					}
				}
		}
		if ($limitreached) echo "<br/>
			<div style=\"text-align:right\" id=\"expander\"><a href=\"javascript:expand_news(", $offset+1,");\">Fler nyheter &gt;&gt;</a></div>";
	break;

	case "videos":

		if ($category) {
			$low = $category."00";
			$high = ($category+1)."00";
			if ($sub) $getcatvideos = mysql_query("SELECT `video_id` FROM `videos_groups` WHERE `group_id` = '$category"."$sub'");
			else $getcatvideos = mysql_query("SELECT `video_id` FROM `videos_groups` WHERE `group_id` BETWEEN $low AND $high");
			while ($gcv = mysql_fetch_assoc($getcatvideos)) {
				$buffer[] = $gcv['video_id'];
			}
			if ($buffer) {
				$categories = implode(",",$buffer);
				$buffer=null;
				$catsql = "`id` IN($categories) AND";
			}
			else die("Inga videos hittades"); //inga videos hittades
		}

		$get=mysql_query("SELECT SQL_CALC_FOUND_ROWS `id`, `title`, `text_source`, `description`, `description2`, `date_start`, `user_id`, `url`, `mainlink`, `mainlink_activated` FROM `videos` WHERE $catsql `approved`='1' AND `deleted`='0' AND NOW() > `date_start` ORDER BY `title` ASC, `id` DESC LIMIT 0, ". ($offset * 60)) or die(mysql_error());
		while ($gn = mysql_fetch_assoc($get)) {
				if (!$latest) $latest = $gn;

				$description = strip_tags($gn['description'])." ".strip_tags($gn['description2']);
				$title = substr($gn['title'],0,80);
				$lentitle = strlen(strip_tags($title));

				if (strlen($description) + $lentitle > 140) $description = substr($description,0,(140 - $lentitle));

				echo "<div style=\"padding:4px\">";
				//echo "<small>".substr($gn['date_start'],0,10)."</small><br/>";
				echo "<a href=\"?page=videos";
				if ($category) echo "&cat=$category";
				if ($sub) echo "&sub=$sub";
				echo "&id=$gn[id]\"><b>".strip_tags($title)."</b>. $description &gt;&gt;</a></div>";
			}
}

?>